/// 采购商品数据模型
class PurchaseItemModel {
  /// 商品ID
  String? itemId;
  
  /// 商品名称
  String? name;
  
  /// 商品图片
  String? image;
  
  /// 商品价格
  double? price;
  
  /// 商品规格
  String? specification;
  
  /// 商品描述
  String? description;
  
  /// 商品分类
  String? category;
  
  /// 供应商
  String? supplier;
  
  /// 库存数量
  int? stock;
  
  /// 商品状态 1:上架 0:下架
  int? status;
  
  /// 构造函数
  PurchaseItemModel({
    this.itemId,
    this.name,
    this.image,
    this.price,
    this.specification,
    this.description,
    this.category,
    this.supplier,
    this.stock,
    this.status,
  });
  
  /// 从JSON创建对象
  factory PurchaseItemModel.fromJson(Map<String, dynamic> json) {
    return PurchaseItemModel(
      itemId: json['item_id']?.toString(),
      name: json['name']?.toString(),
      image: json['image']?.toString(),
      price: json['price'] is double ? json['price'] : double.tryParse(json['price']?.toString() ?? ''),
      specification: json['specification']?.toString(),
      description: json['description']?.toString(),
      category: json['category']?.toString(),
      supplier: json['supplier']?.toString(),
      stock: json['stock'] is int ? json['stock'] : int.tryParse(json['stock']?.toString() ?? ''),
      status: json['status'] is int ? json['status'] : int.tryParse(json['status']?.toString() ?? ''),
    );
  }
  
  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'item_id': itemId,
      'name': name,
      'image': image,
      'price': price,
      'specification': specification,
      'description': description,
      'category': category,
      'supplier': supplier,
      'stock': stock,
      'status': status,
    };
  }
}

/// 采购订单数据模型
class PurchaseOrderModel {
  /// 订单ID
  String? orderId;
  
  /// 用户ID
  String? userId;
  
  /// 订单状态 1:待支付 2:已支付 3:已发货 4:已完成 5:已取消
  int? status;
  
  /// 订单总金额
  double? totalAmount;
  
  /// 收货地址
  AddressModel? address;
  
  /// 订单商品列表
  List<PurchaseOrderItemModel>? items;
  
  /// 创建时间
  String? createTime;
  
  /// 更新时间
  String? updateTime;
  
  /// 备注
  String? remark;
  
  /// 构造函数
  PurchaseOrderModel({
    this.orderId,
    this.userId,
    this.status,
    this.totalAmount,
    this.address,
    this.items,
    this.createTime,
    this.updateTime,
    this.remark,
  });
  
  /// 从JSON创建对象
  factory PurchaseOrderModel.fromJson(Map<String, dynamic> json) {
    return PurchaseOrderModel(
      orderId: json['order_id']?.toString(),
      userId: json['user_id']?.toString(),
      status: json['status'] is int ? json['status'] : int.tryParse(json['status']?.toString() ?? ''),
      totalAmount: json['total_amount'] is double ? json['total_amount'] : double.tryParse(json['total_amount']?.toString() ?? ''),
      address: json['address'] != null ? AddressModel.fromJson(json['address']) : null,
      items: json['items'] != null 
          ? (json['items'] as List).map((item) => PurchaseOrderItemModel.fromJson(item)).toList()
          : null,
      createTime: json['create_time']?.toString(),
      updateTime: json['update_time']?.toString(),
      remark: json['remark']?.toString(),
    );
  }
  
  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'order_id': orderId,
      'user_id': userId,
      'status': status,
      'total_amount': totalAmount,
      'address': address?.toJson(),
      'items': items?.map((item) => item.toJson()).toList(),
      'create_time': createTime,
      'update_time': updateTime,
      'remark': remark,
    };
  }
}

/// 采购订单商品数据模型
class PurchaseOrderItemModel {
  /// 商品ID
  String? itemId;
  
  /// 商品名称
  String? name;
  
  /// 商品图片
  String? image;
  
  /// 商品价格
  double? price;
  
  /// 购买数量
  int? quantity;
  
  /// 小计金额
  double? subtotal;
  
  /// 构造函数
  PurchaseOrderItemModel({
    this.itemId,
    this.name,
    this.image,
    this.price,
    this.quantity,
    this.subtotal,
  });
  
  /// 从JSON创建对象
  factory PurchaseOrderItemModel.fromJson(Map<String, dynamic> json) {
    return PurchaseOrderItemModel(
      itemId: json['item_id']?.toString(),
      name: json['name']?.toString(),
      image: json['image']?.toString(),
      price: json['price'] is double ? json['price'] : double.tryParse(json['price']?.toString() ?? ''),
      quantity: json['quantity'] is int ? json['quantity'] : int.tryParse(json['quantity']?.toString() ?? ''),
      subtotal: json['subtotal'] is double ? json['subtotal'] : double.tryParse(json['subtotal']?.toString() ?? ''),
    );
  }
  
  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'item_id': itemId,
      'name': name,
      'image': image,
      'price': price,
      'quantity': quantity,
      'subtotal': subtotal,
    };
  }
}

/// 地址信息数据模型
class AddressModel {
  /// 地址ID
  String? addressId;
  
  /// 收货人姓名
  String? receiverName;
  
  /// 收货人电话
  String? receiverPhone;
  
  /// 省份
  String? province;
  
  /// 城市
  String? city;
  
  /// 区县
  String? district;
  
  /// 详细地址
  String? detailAddress;
  
  /// 邮政编码
  String? postalCode;
  
  /// 是否默认地址
  bool? isDefault;
  
  /// 构造函数
  AddressModel({
    this.addressId,
    this.receiverName,
    this.receiverPhone,
    this.province,
    this.city,
    this.district,
    this.detailAddress,
    this.postalCode,
    this.isDefault,
  });
  
  /// 从JSON创建对象
  factory AddressModel.fromJson(Map<String, dynamic> json) {
    return AddressModel(
      addressId: json['address_id']?.toString(),
      receiverName: json['receiver_name']?.toString(),
      receiverPhone: json['receiver_phone']?.toString(),
      province: json['province']?.toString(),
      city: json['city']?.toString(),
      district: json['district']?.toString(),
      detailAddress: json['detail_address']?.toString(),
      postalCode: json['postal_code']?.toString(),
      isDefault: json['is_default'] == true || json['is_default'] == 1,
    );
  }
  
  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'address_id': addressId,
      'receiver_name': receiverName,
      'receiver_phone': receiverPhone,
      'province': province,
      'city': city,
      'district': district,
      'detail_address': detailAddress,
      'postal_code': postalCode,
      'is_default': isDefault,
    };
  }
  
  /// 获取完整地址
  String get fullAddress {
    return '${province ?? ''}${city ?? ''}${district ?? ''}${detailAddress ?? ''}';
  }
}